package org.moonlight.jvm.instructions.math.arithmetic;

import lombok.Setter;
import org.moonlight.jvm.instructions.base.BytecodeReader;
import org.moonlight.jvm.instructions.base.Instruction;
import org.moonlight.jvm.rtda.exclusive.Frame;
import org.moonlight.jvm.rtda.exclusive.LocalVars;

/**
 * IINC: iinc指令是给 局部变量表 中的 int变量增加常量值，然后再把结果写回 局部变量表.
 *       局部变量表索引和常量值都由指令的操作数提供。
 *
 * @author Moonlight
 * @createTime 2023/8/16 17:54
 **/
@Setter
public class IInc implements Instruction {

    private int idx;
    private int constVal;

    @Override
    public void fetchOperands(BytecodeReader reader) {
        this.idx = reader.readInt8();
        this.constVal = reader.readInt8();
    }

    @Override
    public void execute(Frame frame) {
        LocalVars localVars = frame.getLocalVars();
        int val = localVars.getInt(this.idx);
        val += this.constVal;
        localVars.setInt(this.idx, val);
    }
}
